<%
   limit = widget_properties["numberOfLines"]
   metric_prefix = dashboard_configuration.selected_period? ? 'new_' : ''
   value_column = (dashboard_configuration.selected_period? ? "variation_value_#{dashboard_configuration.period_index}" : 'value')
   measures_by_severity = {}
   all_measures=[]

   Severity::KEYS.each do |key|
     rule_measures = @snapshot.rule_measures(Metric.by_key("#{metric_prefix}#{key.downcase}_violations"))
     sorted_measures = rule_measures.select { |a|
       val = a.send(value_column)
       val && val>0
     }.sort { |a, b| b.send(value_column) <=> a.send(value_column) }[0...limit]
     measures_by_severity[key] = sorted_measures
     all_measures.concat(sorted_measures)
   end
   measures_by_severity['all']=all_measures.sort { |a, b| b.send(value_column) <=> a.send(value_column) }[0...limit]

   default_severity = widget_properties['defaultSeverity']
   default_severity = 'all' unless Severity::KEYS.include?(default_severity)
%>

<script type="text/javascript">
  function showMostViolatedRules<%= widget.id -%>(severity) {
    $j('#block_<%= widget.id-%> div.hotspot').hide();
    $j('#most-violated-rules-<%= widget.id -%>-' + severity).show();
  }

  function severityForLink() {
    var severity = $j('#select-severity_<%= widget.id -%>').val();
    if (severity == 'all') {
      return '';
    } else {
      return severity;
    }
  }
</script>

<div class="line-block">
  <div style="float:right">
    <a class="underlined-link" href="#" onclick="window.location.href='<%= url_for(:controller => 'drilldown', :action => 'issues') -%>/<%= @resource.id -%>?period=<%= dashboard_configuration.period_index -%>&severity=' + severityForLink() ">
      <%= message('widget.hotspot_metric.more') -%>
    </a>
  </div>
  <h3>
    <%= message(dashboard_configuration.selected_period? ? 'widget.hotspot_most_violated_rules.name_when_period' : 'widget.hotspot_most_violated_rules.name') %>

    <select class="small withIcons" style="margin-left: 20px" onchange="showMostViolatedRules<%= widget.id -%>(this.value);" id="select-severity_<%= widget.id -%>">
      <option value="all" <%= 'selected' if default_severity=='all' -%>><%= message('widget.hotspot_most_violated_rules.any_severity') -%></option>
      <% Severity::KEYS.each do |key| %>
        <option class="sev_<%= key -%>" value="<%= key -%>" <%= 'selected' if default_severity==key -%>><%= message("severity.#{key}") -%></option>
      <% end %>
    </select>

    <script>
      $j(function() {
        $j('#select-severity_<%= widget.id -%>').select2({ width: '130px' });
      });
    </script>
  </h3>
</div>


<div>
  <%
     measures_by_severity.each do |severity, measures|
       if measures.empty?
  %>

      <div id="most-violated-rules-<%= widget.id -%>-<%= severity -%>" class="hotspot" style="padding-top:10px">
        <span class="empty_widget"><%= message('widget.hotspot_most_violated_rules.no_violation_for_severity') -%></span>
      </div>

    <%
       else
    %>

      <div id="most-violated-rules-<%= widget.id -%>-<%= severity -%>" class="hotspot">
        <table class="data">
          <thead>
          <tr>
            <th colspan="3"></th>
          </tr>
          </thead>
          <tbody>
          <%
             max_value = measures.first.send(value_column)
             measures.each do |m|
               rule = m.rule
               value = m.send(value_column)
          %>
            <tr class="<%= cycle 'even', 'odd', :name => ("hotspot_most_violated_rules-#{widget.id}-#{severity}") -%>">
              <td class="thin">
                <i class="icon-severity-<%= m.severity.downcase -%>"></i>
              </td>
              <td>
                <%= h rule.name -%>
              </td>
              <td class="right nowrap">
                <a class="underlined-link <%= widget.key %>_<%= rule.key.parameterize %>" href="<%= url_for(:controller => 'drilldown', :action => 'issues', :id => @resource.key, :rule => rule.key, :rule_sev => m.severity, :period => dashboard_configuration.period_index) -%>">
                  <%= dashboard_configuration.selected_period? ? format_variation(m) : format_measure(m) -%>
                </a>
              </td>
              <td class="barchart">
                <div class="barchart" style="width: <%= (value*100/max_value).round.to_i -%>%">
                  <div style="width: 100%;"></div>
                </div>
              </td>
            </tr>
          <%
             end
          %>
          </tbody>
        </table>
      </div>

    <%
       end
       end
    %>
</div>
<script type="text/javascript">
  showMostViolatedRules<%= widget.id -%>("<%= default_severity -%>");
</script>
